import { _decorator, Component, Label, log, Node, sys, Toggle, Tween, tween, Vec3 } from 'cc';
import { StaminaSystem } from './StaminaSystem';
import { losChannel } from '../SDK/losChannel';
import loskc from '../SDK/loskc';
const { ccclass, property } = _decorator;

@ccclass('InfiniteStaminaUI')
export class InfiniteStaminaUI extends Component {
    @property(Toggle)
    public toggle: Toggle = null;
    @property(Node)
    public btnCancel: Node = null;
    @property(Node)
    public btnGet: Node = null;
    @property(Label)
    public labelTimes: Label = null;
    @property(Node)
    toggleNode: Node = null;

    @property(Node)
    private videoSp: Node = null!;
    @property(Node)
    private videoLb: Node = null!;

    private usetimes: number = 0;
    private _needWatchVideo: boolean = true;
    private _tw: Tween<Node> = null;

    private _currentVideoNode: Node = null;

    start() {
        this._currentVideoNode = this.videoSp;
        this.videoSp.active = true;
        this.videoLb.active = false;
        const times = sys.localStorage.getItem('infiniteStaminaUseTimes')
        if (times) {
            this.usetimes = parseInt(times);
        }
        losChannel.instance().doAction(22, ['hw'], (enabled, val) => {
            if (enabled) {
                this.videoSp.active = false;
                this.videoLb.active = true;
                this._currentVideoNode = this.videoLb;
            }
            else {
                this.videoLb.active = false;
                this.videoSp.active = true;
                this._currentVideoNode = this.videoSp;
            }

        });
        losChannel.instance().doAction(1, ['ks'], (enabled, val) => {
            this.toggle.isChecked = enabled
        })
        losChannel.instance().doAction(1, ['hw', 'ks2', 'ks3', 'honor'], (enabled, val) => {
            switch (val) {
                case 0:
                    {
                        this._currentVideoNode.active = false;
                        this.toggleNode.active = false;
                        this._needWatchVideo = false;
                    }
                    break;
                case 1:
                    {
                        this._currentVideoNode.active = true;
                        this.toggleNode.active = true;
                        this._needWatchVideo = true;
                        this.toggle.isChecked = true
                    }
                    break;
                case 2:
                    {
                        this._currentVideoNode.active = true;
                        this.toggleNode.active = false;
                        this._needWatchVideo = true;
                        this.toggle.isChecked = true
                    }
                    break;
            }
        })
        this.clickToggleFunc();
        this._tw = tween(this.btnGet).sequence
            (
                tween(this.btnGet).to(0.4, { scale: new Vec3(1.12, 1.12, 1.12) }),
                tween(this.btnGet).to(0.4, { scale: new Vec3(1, 1, 1) })
            )
            .repeatForever()
            .start()
    }

    update(deltaTime: number) {

    }
    /**
     * onCancelClick
     */
    public onCancelClick() {
        this.node.removeFromParent();
    }
    /**
     * onGetClick
     */
    public onGetClick() {
        if (this.usetimes >= 3)
            return

        const getLogic = () => {
            StaminaSystem.instance.setInfiniteStamina(24)
            this.usetimes++
            sys.localStorage.setItem('infiniteStaminaUseTimes', this.usetimes.toString())
            this.labelTimes.string = `(${this.usetimes}/3)`
            if (this._tw)
                this._tw.stop()
            this.node.removeFromParent()
        };
        if (this._needWatchVideo) {
            losChannel.instance().showVideo({
                sucCall: () => {
                    getLogic();
                },
                failCall: () => {
                },
                endCall: () => {
                }
            })
        }
        else {
            getLogic();
        }
    }
    public onToggleClick(event: Event, customEventData: string) {
        this.clickToggleFunc();
    }
    private clickToggleFunc() {
        const chName = losChannel.instance().channel;
        switch (chName) {
            case 'ks1':
                {
                    this.updateDropButton()
                }
                break;
            case 'ks2':
            case 'ks3':
            case 'hw':
            case 'honor':
                {
                    this.updatevideoSp();
                }
            default:
                {
                    this.updatevideoSp();
                }
                break;
        }
        this.labelTimes.string = `(${this.usetimes}/3)`
    }
    private updateDropButton() {
        if (this.toggle.isChecked) {
            this.btnCancel.active = false
            this.btnGet.setPosition(0, 0)
        }
        else {
            this.btnCancel.active = true
            this.btnGet.setPosition(130, 0)
        }
    }
    private updatevideoSp() {
        this._currentVideoNode.active = this.toggle.isChecked;
        this._needWatchVideo = this.toggle.isChecked;
    }
    protected onEnable(): void {
        losChannel.instance().doAction('所有二级界面弹出插屏', ['hw']);


    }
}